package com.huawei.hianalytics.log.g;

import android.os.Build;
import android.os.Bundle;
import com.huawei.fastengine.BuildConfig;
import com.huawei.hianalytics.c.a;
import com.huawei.hianalytics.log.e.b;
import com.huawei.hianalytics.log.e.e;
import com.huawei.hianalytics.util.f;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import o.cfv;
import org.json.JSONException;
import org.json.JSONObject;

public class d implements e {
    private String a;
    private String b;
    private Bundle c;

    public d(Bundle bundle, String str, String str2) {
        this.c = bundle;
        this.a = str;
        this.b = str2;
    }

    private void a() {
        Closeable fileOutputStream;
        Closeable bufferedWriter;
        Throwable th;
        Closeable closeable = null;
        File file = new File(this.b, this.a);
        if (file.length() <= 25600 || !file.delete()) {
            String string = this.c.getString("MetaData");
            String string2 = this.c.getString("Eventid");
            CharSequence a = e.a(string2, string);
            String str = "";
            String str2 = "";
            string = "";
            a b = com.huawei.hianalytics.d.a.a().b();
            switch (b.a()) {
                case UDID:
                    str2 = b.b();
                    break;
                case IMEI:
                    str = b.b();
                    break;
                case SN:
                    string = b.b();
                    break;
            }
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.putOpt("LogVersion", cfv.VER_NUMBER);
                jSONObject.putOpt("LogSubversion", BuildConfig.VERSION_NAME);
                jSONObject.putOpt("ProductVersion", f.a());
                jSONObject.putOpt("IMEI", b.a(str));
                jSONObject.putOpt("udid", b.a(str2));
                jSONObject.putOpt("sn", b.a(string));
                jSONObject.putOpt("uuid", b.a(com.huawei.hianalytics.d.a.a().d()));
                jSONObject.putOpt("Eventid", string2);
                jSONObject.putOpt("HappenTime", "" + System.currentTimeMillis());
                jSONObject.putOpt("App-Id", com.huawei.hianalytics.d.a.a().e());
                jSONObject.putOpt("App-Ver", com.huawei.hianalytics.d.a.a().f());
                jSONObject.putOpt("Sdk-Name", "hianalytics");
                jSONObject.putOpt("Sdk-Ver", "2.1.4.312");
                jSONObject.putOpt("Device-Type", Build.MODEL);
                jSONObject.putOpt("Package-Name", com.huawei.hianalytics.a.b.e());
            } catch (JSONException e) {
                com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog(): json Exception!Some parameters are added to the error");
            }
            str = jSONObject.toString();
            try {
                fileOutputStream = new FileOutputStream(file);
                try {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "UTF-8"));
                    try {
                        bufferedWriter.append(str + '\n');
                        bufferedWriter.append(a);
                        bufferedWriter.flush();
                        com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                        com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    } catch (FileNotFoundException e2) {
                        closeable = fileOutputStream;
                        try {
                            com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,file not found!");
                            com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                            com.huawei.hianalytics.log.e.d.a(0, closeable);
                            if (file.length() > 25600) {
                                return;
                            }
                            return;
                        } catch (Throwable th2) {
                            Throwable th3 = th2;
                            fileOutputStream = closeable;
                            closeable = bufferedWriter;
                            th = th3;
                            com.huawei.hianalytics.log.e.d.a(9, closeable);
                            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                            throw th;
                        }
                    } catch (UnsupportedEncodingException e3) {
                        try {
                            com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog, Unsupported coding format!");
                            com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                            if (file.length() > 25600) {
                                return;
                            }
                            return;
                        } catch (Throwable th4) {
                            closeable = bufferedWriter;
                            th = th4;
                            com.huawei.hianalytics.log.e.d.a(9, closeable);
                            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                            throw th;
                        }
                    } catch (IOException e4) {
                        com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,BufferedWriter IO Exception!");
                        com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                        com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                        if (file.length() > 25600) {
                            return;
                        }
                        return;
                    }
                } catch (FileNotFoundException e5) {
                    bufferedWriter = null;
                    closeable = fileOutputStream;
                    com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,file not found!");
                    com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                    com.huawei.hianalytics.log.e.d.a(0, closeable);
                    if (file.length() > 25600) {
                        return;
                    }
                    return;
                } catch (UnsupportedEncodingException e6) {
                    bufferedWriter = null;
                    com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog, Unsupported coding format!");
                    com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    if (file.length() > 25600) {
                        return;
                    }
                    return;
                } catch (IOException e7) {
                    bufferedWriter = null;
                    com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,BufferedWriter IO Exception!");
                    com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    if (file.length() > 25600) {
                        return;
                    }
                    return;
                } catch (Throwable th5) {
                    th = th5;
                    com.huawei.hianalytics.log.e.d.a(9, closeable);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    throw th;
                }
            } catch (FileNotFoundException e8) {
                bufferedWriter = null;
                com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,file not found!");
                com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                com.huawei.hianalytics.log.e.d.a(0, closeable);
                if (file.length() > 25600) {
                    return;
                }
                return;
            } catch (UnsupportedEncodingException e9) {
                bufferedWriter = null;
                fileOutputStream = null;
                com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog, Unsupported coding format!");
                com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                if (file.length() > 25600) {
                    return;
                }
                return;
            } catch (IOException e10) {
                bufferedWriter = null;
                fileOutputStream = null;
                com.huawei.hianalytics.g.b.d("LoginfoWrite", "writerEventLog,BufferedWriter IO Exception!");
                com.huawei.hianalytics.log.e.d.a(9, bufferedWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                if (file.length() > 25600) {
                    return;
                }
                return;
            } catch (Throwable th6) {
                th = th6;
                fileOutputStream = null;
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                throw th;
            }
            if (file.length() > 25600 && file.delete()) {
                com.huawei.hianalytics.g.b.d("LoginfoWrite", "EventInfo file is overrun and has been deleted");
                return;
            }
            return;
        }
        com.huawei.hianalytics.g.b.d("LoginfoWrite", "EventInfo file is overrun and has been deleted");
    }

    public void run() {
        a();
    }
}
